import {
  getArtistTopSong,
  getArtistDetail,
  getArtistMv,
  getArtistDesc,
  getSimiArtist
} from "@/api/artist/index";

import {
  getPlaylistCatlist,
  getTopPlaylist,
  getAlbumNew,
  getAlbumNewest
} from "@/api/home/index";

// 热门歌手和分类歌手
import {getAtistlist, getTopArtists} from "@/api/singer/index";

const songState = {
  songDetails: {},
  songTabIndex: 0 // 设置歌手详情tab的下标，保证在回退时也会跟随触发
}
const songActions = {
  // 获取歌手热门50条歌曲
  getArtistTopSong(id) {
    return getArtistTopSong(id).then(res => {
      const { code, songs } = res
      if (Number(code) === 200) {
        // console.log(res)
        return songs
      }
    })
  },
  // 播放歌手50首歌
  playArtistTopSong(id, flag = false) {
    getArtistTopSong(id).then(async res => {
        const { code, songs } = res
        if (Number(code) === 200) {
          // 判断是追加歌单还是，播放歌单
          await this.setDetachment(songs, flag)
        }
    })
  },
  // 获取歌手描述
  getArtistDetail(id) {
    getArtistDetail(id).then(res => {
     const { code, data } = res
     if (Number(code) === 200) {
       this.songDetails = data
     }
    })
  },
  // 获取歌手视频用于渲染
  getArtistMv(id) {
    return getArtistMv(id).then(res => {
      const { code, mvs } = res
      if(Number(code) === 200)
      return mvs
    })
  },
  // 获取艺人介绍
  getArtistDesc(id) {
    return getArtistDesc(id).then(res => {
      if (Number(res.code) === 200) {
        return res
      }
    })
  },
  // 获取相似歌手
  getSimiArtist(id) {
   return getSimiArtist(id).then(res => {
     const { code, artists } = res
     if (Number(code) === 200) {
       return artists
     }
    })
  },
  // 歌单分类弹窗数据
  getPlaylistCatlist() {
    return getPlaylistCatlist().then(catlist => {
      if (Number(catlist.code) === 200) {
        // 由于分类的类型和风格是分开的为了方便只能整合在一起
        let categories = []
        // 遍历catlist.categories 他是个对象 { 0:语种 ... }
        for (const catlistKey in catlist.categories) {
          // 这个type就是为了将一个风格和他对应的歌单类型整合在一起然后push到创建的categories中
          let type = {
            title: catlist.categories[catlistKey],
            sub: []
          }
          // sub中存储的是数组对象是对应的catlist.categories
          // 中的值它的下标如果和sub中每个对象的category相等的话就属于当前风格
          catlist.sub.forEach((items) => {
            if (items.category === Number(catlistKey)) {
              type.sub.push(items)
            }
          })
          categories.push(type)
        }
        return {
          catlist: catlist.all,
          categories
        }
      }
    })
  },
  // 获取歌单分类数据 {cat = '全部', limit = 50, offset = 0, order = 'hot'}
  // order: 可选值为 'new' 和 'hot', 分别对应最新和最热 , 默认为 'hot'
  getTopPlaylist(params, num = 0) {
    return getTopPlaylist({...params,limit: 50, offset: 50 * num }).then(res => {
      if(Number(res.code) === 200) {
        return res
      }
    })
  },
  // 全部新碟支持分页数据
  getAlbumNew(params = { limit: 30, num: 0, area: 'ALL' }) {
   return getAlbumNew(params.limit, params.num * params.limit, params.area).then(res => {
      if (Number(res.code) === 200) {
        return res
      }
    })
  },
  // 热门新碟
  getAlbumNewest() {
    return getAlbumNewest().then(res => {
      // console.log(res)
      const { albums, code } = res
      if (Number(code) === 200) {
        return albums
      }
    })
  },
  // 获取热门歌手
  // {limit: 30, offset: 0}
  getTopArtists(num = 0) {
    return getTopArtists({ limit: 50, offset: 30 * num }).then(res => {
      const { artists, code } = res
      if (Number(code) === 200) {
        return artists
      }
    })
  },
  // 获取歌手分类数据
  // // { type: 1(type值是男歌手，女歌手等), area:96（华语欧美等）, initial:b, limit: 30, offset: 0 }
  getAtistlist(params, num = 0) {
   return getAtistlist({ ...params, limit: 30, offset: 30 * num }).then(res => {
      const { code, artists } = res
      if (Number(code) === 200) {
        return artists
      }
    })
  }
}
const songGetters = {
}

export {
  songState,
  songGetters,
  songActions
}
